
function sortF(a, b) {
    return b.F - a.F;
}

function countAroundPointBusy(curPoint,maps){
    var x = curPoint.x, y = curPoint.y;
    let res=[];

    if(x%2===0){
        res=  [
            { x: x - 1, y: y },
            { x: x - 1, y: y + 1 },
            { x: x, y: y + 1 },
            { x: x + 1, y: y + 1 },
            { x: x + 1, y: y },
            { x: x, y: y - 1 }
        ]
    }
    else{
        res= [
            { x: x - 1, y: y - 1 },
            { x: x - 1, y: y },
            { x: x, y: y + 1 },
            { x: x + 1, y: y },
            { x: x + 1, y: y - 1 },
            { x: x, y: y - 1 }
        ]
    }

    return res.filter((r)=>{
        return r.x<maps.length&&r.x>=0&&r.y<maps[0].length&&r.y>=0&&maps[r.x][r.y]===1;
    });    
}


//获取当前坐标点周围点的坐标
function countAroundPoint(curPoint) {

    var x = curPoint.x, y = curPoint.y;
    if(x%2===0){
        return  [
            { x: x - 1, y: y },
            { x: x - 1, y: y + 1 },
            { x: x, y: y + 1 },
            { x: x + 1, y: y + 1 },
            { x: x + 1, y: y },
            { x: x, y: y - 1 }
        ]
    }
    else{
        return [
            { x: x - 1, y: y - 1 },
            { x: x - 1, y: y },
            { x: x, y: y + 1 },
            { x: x + 1, y: y },
            { x: x + 1, y: y - 1 },
            { x: x, y: y - 1 }
        ]
    }

   
}
//返回列表中指定点的序号
function existList(point, list) {
    for (var i in list) {
        if (point.x == list[i].x && point.y == list[i].y) {
            return i;
        }
    }
    return 0;
}

function searchRoad(map,start_x, start_y, end_x, end_y) {

    var openList = [],    //开启列表
        closeList = [],   //关闭列表
        result = [],      //结果数组
        result_index,   //结果数组在开启列表中的序号
        mapRows = map[0].length, //地图行数与列数
        mapColumns = map.length;

    openList.push({ x: start_x, y: start_y, G: 0 });

    do {

        var currentPoint = openList.pop();

        closeList.push(currentPoint);

        var surroundPoint = countAroundPoint(currentPoint);

        for (var i in surroundPoint) {

            let item:any = surroundPoint[i];
                       
            if (

                item.x >= 0 &&                            //判断是否在地图上
                item.y >= 0 &&
                item.x <  mapColumns&&
                item.y < mapRows &&
                map[item.x][item.y] != 1 &&         //判断是否是障碍物
                !existList(item, closeList)) {
            
                var g = currentPoint.G + ((currentPoint.x - item.x) * (currentPoint.y - item.y) == 0 ? 1 : 2);

                if (!existList(item, openList)) {       //如果不在开启列表中
                    item['H'] = Math.abs(end_x - item.x)  + Math.abs(end_y - item.y) ;
                    item['G'] = g;
                    item['F'] = item.H + item.G;
                    item['parent'] = currentPoint;
                    openList.push(item);
         
                }
                else {                                  //存在在开启列表中，比较目前的g值和之前的g的大小
                    var index = existList(item, openList);
                    if (g < openList[index].G) {
                        openList[index].parent = currentPoint;
                        openList[index].G = g;
                        openList[index].F = g + openList[index].H;
                    }

                }
            }
        }
      //  cc.log(openList)
        //如果开启列表空了，没有通路，结果为空
        if (openList.length == 0) {
            break;
        }

        openList.sort(sortF);

    } while (!(result_index = existList({ x: end_x, y: end_y }, openList)));

    //判断结果列表是否为空
    if (!result_index) {

        // result = [];
        
    } else {

        var currentObj = openList[result_index];
        
        do {
            //把路劲节点添加到result当中
            result.unshift({
                x: currentObj.x,
                y: currentObj.y
            });
            currentObj = currentObj.parent;
        } while (currentObj.x != start_x || currentObj.y != start_y);

    }
    // cc.log(result);
    return result;

}

export default{
    searchRoad,
    countAroundPoint,
    countAroundPointBusy
}